const chromeMng = require('../mng/chromeMng')
const logger = require('log4js').getLogger()
const config = require('../util/config')

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, async (reqObj, resolve) => {
        if (!reqObj.link) {
            logger.warn('[' + __filename + ':' + __line + ']', 'lack link')
            resolve({
                result: -1,
                msg: '缺少link'
            })
            return
        }
        if (config.domain == '') {
            chromeMng.getRedirectLink({
                link: reqObj.link,
                test(link) {
                    return link.startsWith('https://detail.tmall.com/item.htm') ||
                        link.startsWith('https://detail.m.tmall.com/item.htm') ||
                        link.startsWith('https://item.taobao.com/item.htm') ||
                        link.startsWith('https://login.tmall.com/') ||
                        link.startsWith('https://traveldetail.fliggy.com/item.htm') ||
                        link.startsWith('https://detail.tmall.hk') ||
                        link.startsWith('http://chaoshi.detail.tmall.com/item.htm') ||
                        link.startsWith('https://chaoshi.detail.tmall.com/item.htm') ||
                        link.startsWith('https://pages.tmall.com/wow/fsp/act/sample-offering')
                }
            }).then(result => {
                resolve({
                    result: 1,
                    actualLink: result
                })
            }, result => {
                if (result)
                   logger.error('[' + __filename + ':' + __line + ']', result)
                resolve({
                    result: -1,
                    msg: result
                })
            })
        } else if (config.domain == 'zoro.site') {
            resolve({
                result: -1,
                msg: '系统错误'
            })
        }
    })
}